package com.queen.restaurant.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.queen.restaurant.base.BaseMessage;
import com.queen.restaurant.domain.Order;
import com.queen.restaurant.service.OrderService;

@Controller
public class OrderController {

	@Autowired
	private OrderService orderService;

	// 创建订单
	@RequestMapping(value = "/order/create_order", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> createOrder(
			@RequestParam("userId") String userId,
			@RequestParam("tableId") String tableId) {
		int result = BaseMessage.ERROR;
		Order order = null;
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			order = orderService.createOrder(userId, tableId);
			result = BaseMessage.SUCCESS;
		} catch (Exception e) {
			result = BaseMessage.ERROR;
		}

		resultMap.put("result", result);
		resultMap.put("order", order);
		return resultMap;
	}

	// 提交订单
	@RequestMapping(value = "/order/commit_order", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> commitOrder(
			@RequestParam("order") String orderJson) {
		ObjectMapper mapper = new ObjectMapper();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Order order = null;
		int result = BaseMessage.ERROR;
		try {
			order = mapper.readValue(orderJson, Order.class);
			orderService.commitOrder(order);
			result = BaseMessage.SUCCESS;
		} catch (JsonParseException e) {
			result = BaseMessage.ERROR;
		} catch (JsonMappingException e) {
			result = BaseMessage.ERROR;
		} catch (IOException e) {
			result = BaseMessage.ERROR;
		}
		resultMap.put("result", result);
		resultMap.put("order", order);
		return resultMap;
	}

	// 取消订单
	@RequestMapping(value = "/order/cancel_order", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> cancelOrder(
			@RequestParam("orderId") String orderId) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
//		String result = "fail";
		int result = BaseMessage.ERROR;
		try {
			orderService.cancelOrder(orderId);
			result = BaseMessage.SUCCESS;
		} catch (Exception e) {
			result = BaseMessage.ERROR;
		}
		resultMap.put("result", result);
		return resultMap;
	}

	// 支付订单
	@RequestMapping(value = "/order/pay_order", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> payOrder(
			@RequestParam("order") String orderJson) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		ObjectMapper mapper = new ObjectMapper();
		int result = BaseMessage.ERROR;
		try {
			Order order = mapper.readValue(orderJson, Order.class);
			orderService.payOrder(order);
			result = BaseMessage.SUCCESS;
		} catch (JsonParseException e) {
			result = BaseMessage.ERROR;
		} catch (JsonMappingException e) {
			result = BaseMessage.ERROR;
		} catch (IOException e) {
			result = BaseMessage.ERROR;
		}
		
		resultMap.put("result", result);
		return resultMap;
	}
	
	//获取用户订单
	@RequestMapping(value = "/order/user_order_list", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getUserOrderList(
			@RequestParam("userId") String userId){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = BaseMessage.ERROR;
		List<Order> userOrderList = null;
		userOrderList = orderService.getUserOrderList(userId);
		List<Map<String, Object>> orderMapList = new ArrayList<Map<String, Object>>();
		if (userOrderList != null) {
			result = BaseMessage.SUCCESS;
			for (Order order : userOrderList) {
				Map<String, Object> orderMap = new HashMap<String, Object>();
				orderMap.put("orderId", order.getId());
				orderMap.put("orderStatus", order.getIsPay());
				orderMap.put("orderTime", order.getOrderTime());
				orderMap.put("orderTotal", order.getTotal());
				orderMap.put("restaurantName", order.getRestaurant().getRestaurantName());
				orderMapList.add(orderMap);
			}
		}
		
		resultMap.put("orderMapList", orderMapList);
		resultMap.put("result", result);
		return resultMap;
	}
	
	//根据订单Id获取订单
	@RequestMapping(value = "/order/get_order_byId", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getOrderByOrderId(
			@RequestParam("orderId") String orderId){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = BaseMessage.ERROR;
		
		Order order = null;
		order = orderService.getOrderByOrderId(orderId);
		if (order != null) {
			result = BaseMessage.SUCCESS;
		}
		
		resultMap.put("order", order);
		resultMap.put("result", result);
		return resultMap;
	}
	
}
